package cc.eddic.pratice.programmingparadigms.emp01array.impl;

//import cn.eddic.practice.IntService;
import cc.eddic.pratice.programmingparadigms.Employee;
import cc.eddic.pratice.programmingparadigms.Result;
import cc.eddic.pratice.programmingparadigms.emp01array.EmployeeStatisticsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StatisticsHrjImpl extends ReaderGcsImpl implements EmployeeStatisticsService {

    /**
     * 按性别统计
     *
     * @param employees
     * @return
     */

    public Result[] pivotByGender(Employee[] employees) {
        int index = 0;
        Result[] results = new Result[employees.length];
        for (Employee employee : employees) {
            boolean isFound = false;
            for (Result result : results) {
                if (null == result) {
                    break;
                }
                if (result.getKey().equals(employee.getGender())) {
                    isFound = true;
                    result.setCount(result.getCount() + 1);
                    break;
                }
            }
            if (!isFound) {
                results[index++] = new Result(employee.getGender(), 1);
            }
        }
        Result[] newResults = new Result[index];
        for (int i = 0; i < index; i++) {
            newResults[i] = results[i];
        }
        Result[] a = sort(newResults);
        for (int i = 0; i < index; i++) {
            System.out.println(a[i].getKey() + a[i].getCount());
        }
        return sort(newResults);
    }

    private Result[] sort(Result[] result) {
        Result temp = null;
        for (int i = 1; i <= result.length; i++) {
            for (int n = 0; n < result.length - 1; n++) {
                if (result[n].getCount() < result[n + 1].getCount()) {
                    temp = result[n];
                    result[n] = result[n + 1];
                    result[n + 1] = temp;
                }
            }
        }
        return result;
    }

//        //for (int i = 0; i <= employees.length-1; i++) {
//        for (Employee e : employees) {
//            System.out.println("employees " + e.getGender());
//            for (Result result : results) {
//                System.out.println("result " + result);
//
//                if (null == result) {
//                    break;
//                }
//
//                if (result.getKey().equals(e.getGender())) {
//                    result.setCount(result.getCount() + 1);
//                } else {
//                    result.setKey(e.getGender());
//                    result.setCount(1);
//                }
//            }
//        }
//        System.out.println(results[0]);
//        System.out.println(results[1]);
//        return results;


    /**
     * 按部门统计
     *
     * @param employees
     * @return
     */

    public Result[] pivotByDepartment(Employee[] employees) {
        Result[] results = new Result[employees.length];
        int index = 0;
        for (Employee employee : employees) {
            boolean isFound = false;
            for (Result result : results) {
                if (null == result) {
                    break;
                }
                if (result.getKey().equals(employee.getDepartment())) {
                    result.setCount(result.getCount() + 1);
                    break;
                }
            }
            if (!isFound) {
                results[index++] = new Result(employee.getDepartment());
            }
        }
        Result[] newResults = new Result[index];
        for (int i = 0; i < index; i++) {
            newResults[i] = results[i];
        }
        Result[] a = sort(newResults);
        for (int i = 0; i < index; i++) {
            System.out.println(a[i].getKey() + a[i].getCount());
        }
        return sort(newResults);
    }


    public cc.eddic.pratice.programmingparadigms.Result[] pivotByJobTitle(Employee[] employees) {
        return new cc.eddic.pratice.programmingparadigms.Result[0];
    }

    /**
     * 按职位统计
     *
     * @param employees
     * @return
     */

    public Result[] pivotByjobTitle(Employee[] employees) {
        int index = 0;
        Result[] results = new Result[employees.length];
        for (Employee employee : employees) {
            boolean isFound = false;
            for (Result result : results) {
                if (null == result) {
                    break;
                }
                if (result.getKey().equals(employee.getJobTitle())) {
                    isFound = true;
                    result.setCount(result.getCount() + 1);
                    break;
                }
            }
            if (!isFound) {
                results[index++] = new Result(employee.getJobTitle(), 1);
            }
        }
        Result[] newResults = new Result[index];
        for (int i = 0; i < index; i++) {
            newResults[i] = results[i];
        }
        Result[] a = sort(newResults);
        for (int i = 0; i < index; i++) {
            System.out.println(a[i].getKey() + a[i].getCount());
        }
        return sort(newResults);
    }

}
